package com.lxy.equipment.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxy.common.domain.common.R;
import com.lxy.common.utils.UserContext;
import com.lxy.equipment.domain.po.Equipment;
import com.lxy.equipment.domain.po.Repair;
import com.lxy.equipment.domain.vo.RepairVO;
import com.lxy.equipment.mapper.RepairMapper;
import com.lxy.equipment.service.EquipmentService;
import com.lxy.equipment.service.RepairService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
@RequiredArgsConstructor
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements RepairService {

    private final EquipmentService equipmentService;

    @Override
    public void createRepair(String repairMessage) {
        String[] split = repairMessage.split("\\+");
        Long equipmentId=Long.valueOf(split[0]);
        String remark="";
        if(split.length>1){
            remark = split[1];
        }
        //修改设备状态
        Equipment equipment = equipmentService.getById(equipmentId);
        equipment.setStatus(0);
        equipmentService.updateById(equipment);
        //生成维修订单
        Repair repair=new Repair();
        repair.setEquipmentId(equipmentId);
        repair.setCreateTime(LocalDateTime.now());
        repair.setStatus(0);
        repair.setRemark(remark);
        save(repair);
    }

    @Override
    public R<List<RepairVO>> getMyRepair() {
        //获取用户id
        Long userId = UserContext.getUser();
        //查找数据库
        LambdaQueryWrapper<Repair> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Repair::getUserId,userId);
        List<Repair> repairs = list(lambdaQueryWrapper);
        List<RepairVO> repairVOS=new ArrayList<>();
        for (Repair repair : repairs) {
            //进行封装
            RepairVO repairVO=new RepairVO();
            BeanUtils.copyProperties(repair,repairVO);
            //查找设备名称
            Equipment equipment = equipmentService.getById(repair.getEquipmentId());
            repairVO.setEquipmentName(equipment.getEquipmentName());
            repairVOS.add(repairVO);
        }
        return R.ok(repairVOS);
    }

    @Override
    public R changeRepairStatus(Long repairId) {
//        //查找数据
//        Repair repair = getById(repairId);
        //实际上传来的是设备id
        LambdaQueryWrapper<Repair> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Repair::getEquipmentId,repairId);
        Repair repair = getOne(lambdaQueryWrapper);
        //修改维修单状态
        repair.setStatus(1);
        updateById(repair);
        return R.ok("修改成功，状态：维修中");
    }

    @Override
    public R deleteRepairUser(Long userId) {
        LambdaQueryWrapper<Repair> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Repair::getUserId,userId);
        List<Repair> repairs = list(lambdaQueryWrapper);
        for (Repair repair : repairs) {
            repair.setUserId(0L);
        }
        //更新数据
        updateBatchById(repairs);
        return R.ok("维修任务删除维修人员成功");
    }

    @Override
    public R<List<com.lxy.common.domain.po.Repair>> getAllRepair() {
        List<Repair> Repairs = list();
        List<com.lxy.common.domain.po.Repair> repairLists=new ArrayList<>();
        for (Repair repair : Repairs) {
            com.lxy.common.domain.po.Repair repair1=new com.lxy.common.domain.po.Repair();
            BeanUtils.copyProperties(repair,repair1);
            repairLists.add(repair1);
        }
        return R.ok(repairLists);
    }
}
